package com.threeox.commonlibrary.ui.view.pullrefresh.headfooter;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.threeox.commonlibrary.R;
import com.threeox.commonlibrary.ui.view.inter.pullrefresh.IHeaderFooter;
import com.threeox.utillibrary.util.res.LayoutParamsUtils;
import com.threeox.utillibrary.util.res.LayoutUtils;

/**
 *
 * @ClassName: FooterView
 *
 * @Description: Todo(上拉的View)
 *
 * @author 赵屈犇
 *
 * @date 创建时间:2016/12/22 9:56
 *
 * @version 1.0
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class FooterView extends LinearLayout implements IHeaderFooter {

    /** 底部布局开始 */
    private ImageView mFooterArrow;
    private ProgressBar mFooterCircle;
    private TextView mFooterState;
    private ImageView mFooterStateImg;
    /** 底部布局结束 */

    public FooterView(Context context) {
        this(context, null);
    }

    public FooterView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FooterView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View mFooterView = LayoutUtils.inflate(context, R.layout.view_footer_view);
        mFooterView.setLayoutParams(LayoutParamsUtils.newInstance(context).getLinearLayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT));
        this.addView(mFooterView);
        initView();
    }

    private void initView() {
        mFooterArrow = (ImageView) findViewById(R.id.id_footer_arrow);
        mFooterCircle = (ProgressBar) findViewById(R.id.id_footer_circle);
        mFooterState = (TextView) findViewById(R.id.id_footer_state);
        mFooterStateImg = (ImageView) findViewById(R.id.id_footer_state_img);
    }

    @Override
    public void init(String msg) {
        // 上拉布局初始状态
        mFooterStateImg.setVisibility(View.GONE);
        mFooterCircle.setVisibility(View.GONE);
        mFooterArrow.clearAnimation();
        mFooterArrow.setVisibility(View.VISIBLE);
        mFooterState.setText((TextUtils.isEmpty(msg) ? getContext().getString(R.string.pullup_to_load) : msg));
    }

    @Override
    public void loading(String msg) {
        // 正在加载状态
        // mFooterArrow.clearAnimation();
        mFooterCircle.setVisibility(View.VISIBLE);
        mFooterArrow.setVisibility(View.INVISIBLE);
        mFooterState.setText((TextUtils.isEmpty(msg) ? getContext().getString(R.string.loading) : msg));
    }

    @Override
    public void release(String msg) {
        // 释放加载状态
        mFooterState.setText((TextUtils.isEmpty(msg) ? getContext().getString(R.string.release_to_load) : msg));
    }

    @Override
    public void loadFailed(String msg) {
        // 加载成功
        mFooterCircle.setVisibility(View.INVISIBLE);
        mFooterStateImg.setVisibility(View.VISIBLE);
        mFooterStateImg.setBackgroundResource(R.mipmap.ic_load_failed);
        mFooterState.setText((TextUtils.isEmpty(msg) ? getContext().getString(R.string.load_fail) : msg));
    }

    @Override
    public void loadSucceed(String msg) {
        // 加载失败
        mFooterCircle.setVisibility(View.INVISIBLE);
        mFooterStateImg.setVisibility(View.VISIBLE);
        mFooterStateImg.setBackgroundResource(R.mipmap.ic_load_succeed);
        mFooterState.setText((TextUtils.isEmpty(msg) ? getContext().getString(R.string.load_succeed) : msg));
    }
}
